package com.helloworld.demo.pool;


import java.util.concurrent.*;

public class ThreadPoolTypeTest {




    public static void main(String[] args) {

        ExecutorService executorService1 = Executors.newCachedThreadPool();
        ExecutorService executorService2 = Executors.newFixedThreadPool(10);
        ExecutorService executorService3 = Executors.newSingleThreadExecutor();

        System.out.println(Runtime.getRuntime().availableProcessors());

        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                3,
                5,
                3,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<Runnable>(3),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.DiscardPolicy()
        );


        for(int i = 0;i < 9;i++){
            threadPoolExecutor.execute(new MyTask(i));
        }

        threadPoolExecutor.shutdown();

    }


}

class MyTask implements Runnable{

    private int i = 0;

    public MyTask(int i){
        this.i = i;
    }

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName() + "做了"+ i + "个项目");

        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
